package com.handle.system.web.controller;

import com.handle.common.core.result.JsonResult;
import com.handle.common.core.valid.AddGroup;
import com.handle.common.core.valid.OtherGroup;
import com.handle.common.core.valid.UpdateGroup;
import com.handle.common.log.annotation.OperateLog;
import com.handle.common.log.enums.BusinessType;
import com.handle.common.permissions.annotation.HasPermissions;
import com.handle.system.api.constants.ResponseEnum;
import com.handle.system.api.dto.SysRoleDto;
import com.handle.system.api.entity.SysRole;
import com.handle.system.api.request.role.RoleAddOrUpdateParam;
import com.handle.system.api.request.role.RolePermissionParam;
import com.handle.system.api.request.role.RoleQueryParam;
import com.handle.system.api.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhangqing
 * @description 角色管理控制层
 * @date 2021/7/30 4:12 下午
 */
@RestController
@RequestMapping("/role")
@AllArgsConstructor
@Api(tags = "角色管理", value = "SysRoleController")
public class SysRoleController {
    private final ISysRoleService sysRoleService;

    @ApiOperation("获取租户下所有角色未删除的并且状态为正常")
    @GetMapping("/findRoleByTenantCode/{tenantCode}")
    public JsonResult findRoleByTenantCode(@PathVariable String tenantCode) {
        return JsonResult.success(sysRoleService.findRoleByTenantCode(tenantCode));
    }

    @ApiOperation("查询角色列表(分页查询)")
    @PostMapping("/list")
    public JsonResult list(@RequestBody RoleQueryParam roleQueryParam) {
        return sysRoleService.list(roleQueryParam);
    }

    @ApiOperation("查询角色详情")
    @GetMapping("/roleDetails/{roleId}")
    public JsonResult roleDetails(@PathVariable Long roleId){
        return sysRoleService.roleDetails(roleId);
    }

    @ApiOperation("查询角色列表(分页+排序查询)")
    @PostMapping("/sortList")
    public JsonResult sortList(@RequestBody RoleQueryParam roleQueryParam) {
        return sysRoleService.sortList(roleQueryParam);
    }

    @ApiOperation("添加角色")
    @PostMapping("/addRole")
    @HasPermissions("system:role:add")
    @OperateLog(title = "角色管理-新增角色", businessType = BusinessType.INSERT)
    public JsonResult add(@Validated({AddGroup.class}) @RequestBody RoleAddOrUpdateParam roleAddOrUpdateParam) {
        ResponseEnum.ROLE_CODE_EXISTS.assertIsTrue(sysRoleService.checkRoleCode(roleAddOrUpdateParam.getRoleCode(),
                roleAddOrUpdateParam.getTenantCode()));
        return sysRoleService.add(roleAddOrUpdateParam);
    }

    @ApiOperation("编辑角色")
    @PostMapping("/editRole")
    @HasPermissions("system:role:edit")
    @OperateLog(title = "角色管理-编辑角色", businessType = BusinessType.UPDATE)
    public JsonResult edit(@Validated({UpdateGroup.class}) @RequestBody RoleAddOrUpdateParam roleAddOrUpdateParam) {
        return sysRoleService.edit(roleAddOrUpdateParam);
    }

    @ApiOperation("角色权限")
    @PostMapping("/roleAuthority")
    @HasPermissions("system:role:roleAuthority")
    @OperateLog(title = "角色管理-角色权限", businessType = BusinessType.UPDATE)
    public JsonResult roleAuthority(@Validated({OtherGroup.class}) @RequestBody SysRoleDto sysRoleDto) {
        return sysRoleService.roleAuthority(sysRoleDto);
    }


    @ApiOperation("更新角色状态(启用/禁用)")
    @PutMapping("/changeRoleStatus/{id}/{status}")
    @HasPermissions("system:role:edit")
    @OperateLog(title = "角色管理-更新角色状态", businessType = BusinessType.UPDATE)
    public JsonResult changeRoleStatus(@PathVariable("id") Long id,@PathVariable("status") Boolean status) {
        return sysRoleService.changeRoleStatus(id,status);
    }

    @ApiOperation("批量删除角色")
    @DeleteMapping("/batchDelRole")
    @HasPermissions("system:role:remove")
    @OperateLog(title = "角色管理-更新角色状态", businessType = BusinessType.UPDATE)
    public JsonResult batchDelRole(@RequestBody List<Long> roleIds) {
        return sysRoleService.batchDelRole(roleIds);
    }


    @ApiOperation("权限配置")
    @PostMapping("/permission")
    public JsonResult permission(@RequestBody RolePermissionParam rolePermissionParam) {
        return sysRoleService.permission(rolePermissionParam);
    }
}
